#pragma once

#include "Log.hpp"
// #include"Cond.hpp"
#include "Mutex.hpp"
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <cstring>
#include <sys/wait.h>
#include <signal.h>
#include <sys/types.h>
// #include"Thread.hpp"
// #include"ThreadPool.hpp"
// using namespace CondModule;
// using namespace MutexMudole;
using namespace LogMoudle;
// using namespace ThreadModlue;
// using namespace ThreadPoolMudole;
class NetAddr
{
private:
    struct sockaddr_in addr;
    std::string _ip;
    uint16_t _port;

public:
    NetAddr() = default;
    NetAddr(std::string &ip, uint16_t port) : _port(port), _ip(ip)
    {
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        addr.sin_port = htons(port);
    }
    NetAddr(struct sockaddr_in &addr){
        SetAddr(addr);
    }
    void SetAddr(struct sockaddr_in &addr)
    {
        // this->addr = addr;
        // _ip = inet_ntoa(addr.sin_addr);
        // _port = ntohs(addr.sin_port);
        this->addr = addr;
        _port=ntohs(addr.sin_port);
        char buffer[INET_ADDRSTRLEN];
        inet_ntop(AF_INET, &addr.sin_addr, buffer, INET_ADDRSTRLEN);
        _ip = buffer;
    }
    void setAddr(uint16_t port)
    {
        // 主机转网络
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = INADDR_ANY;
        char ipbuffer[INET_ADDRSTRLEN];
        this->_ip = ipbuffer;
        inet_ntop(AF_INET, &addr.sin_addr, ipbuffer, INET_ADDRSTRLEN);
    }
    std::string stringaddr()
    {
        return _ip + ":" + std::to_string(_port);
    }
    bool operator==(const NetAddr &other) const
    {
        return (this->addr.sin_addr.s_addr == other.addr.sin_addr.s_addr && this->addr.sin_port == other.addr.sin_port);
    }
    int getAddrLen() const { return sizeof(addr); }
    NetAddr(uint16_t port)
    {
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = INADDR_ANY;

        char ipbuffer[INET_ADDRSTRLEN]; // ← 16 字节
        inet_ntop(AF_INET, &addr.sin_addr, ipbuffer, INET_ADDRSTRLEN);
        _ip = ipbuffer;
    }
    std::string getIp() const { return _ip; }
    uint16_t getPort() const { return _port; }
    struct sockaddr_in &getAddr() { return addr; }
};
class NoCopy
{
public:
    NoCopy() {}
    ~NoCopy() {}
    NoCopy(const NoCopy &) = delete;
    const NoCopy &operator=(const NoCopy &) = delete;
};